package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.service.CartInfoService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "购物车控制器")
@RestController
@RequestMapping("api/cart")
public class CartApiController {
    @Autowired
    private CartInfoService cartInfoService;

    @ApiOperation(value = "添加购物车")
    @PostMapping("addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable Long skuId, @PathVariable int skuNum, HttpServletRequest request){
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartInfoService.addToCart(skuId,userId,skuNum);
        return Result.ok();
    }

    @ApiOperation(value = "购物车列表")
    @GetMapping("cartList")
    public Result cartList(HttpServletRequest request){
        //用户id
        String userId = AuthContextHolder.getUserId(request);
        //临时用户id
        String userTempId = AuthContextHolder.getUserTempId(request);
        List<CartInfo> cartList = cartInfoService.getCartList(userId, userTempId);
        return Result.ok(cartList);
    }

    @ApiOperation(value = "更改购物车商品选中状态")
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable Long skuId,@PathVariable Integer isChecked,
                            HttpServletRequest request){
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartInfoService.checkCart(userId,isChecked,skuId);
        return Result.ok();
    }



    @ApiOperation(value = "删除某用户指定的购物车商品")
    @DeleteMapping("deleteCart/{skuId}")
    public Result deleteCart(@PathVariable Long skuId,
                            HttpServletRequest request){
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartInfoService.deleteCartInfo(userId,skuId);
        return Result.ok();
    }

    @ApiOperation(value = "订单列表确认购买商品列表")
    @GetMapping("getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable String userId){

        return  cartInfoService.getCartCheckedList(userId);
    }

    @ApiOperation(value = "更新购物车商品最新价格")
    @GetMapping("loadCartCache/{userId}")
    public Result loadCartCache(@PathVariable("userId")String userId){
        cartInfoService.loadCartCache(userId);
        return Result.ok();
    }

}
